package cn.autumnorange.app.common.provider.cacheannotation.filter;

import cn.autumnorange.app.common.provider.cacheannotation.RedisCacheTarget;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RBucket;
import org.redisson.api.RedissonClient;

@Slf4j
public class StringRedissionFilter extends AbstractRedissionFilter {

  public StringRedissionFilter(RedissonClient redissonClient) {
    super(RedisCacheTarget.STRINGCACHE, redissonClient);
  }

  @Override
  public Object doFilter(String dataType, String dataKey) {
    if (this.getDataType().equalsIgnoreCase(dataType)) {
      RedissonClient redissonClient = this.getRedissonClient();
      RBucket<Object> nameRBucket = redissonClient.getBucket(dataKey);
      if (!nameRBucket.isExists()) {
        return null;
      }
      Object value = nameRBucket.get();
      log.info("cache" + dataKey + ": " + value);

      return value;
    } else {
      return this.getNextRedissionFilter().doFilter(dataType, dataKey);
    }
  }
}
